package com.dikeleakagedetection.myapplication.ui.ble

import android.bluetooth.BluetoothGattCharacteristic
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.RecyclerView
import com.clj.fastble.data.BleDevice
import com.dikeleakagedetection.myapplication.MainActivity
import com.dikeleakagedetection.myapplication.R
import java.util.*

class CharacterAdapter(
    val context: Context,
    private val characterList: MutableList<BluetoothGattCharacteristic>,
    private val bleDevice: BleDevice
) : RecyclerView.Adapter<CharacterAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvCharaTitle = view.findViewById<TextView?>(R.id.tv_chara_title)
        val tvCharaUUID = view.findViewById<TextView?>(R.id.tv_chara_uuid)
        val tvCharaType = view.findViewById<TextView?>(R.id.tv_chara_type)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(
            R.layout.item_character_frag_ble_service,
            parent,
            false
        )

        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bluetoothGattCharacteristic: BluetoothGattCharacteristic = characterList[position]

        //添加特征信息

        //添加特征信息
        val name = "($position)"
        val uuid = bluetoothGattCharacteristic.uuid
        holder.tvCharaTitle.append(name) //添加特征名

        holder.tvCharaUUID.append(uuid.toString()) //添加特征uuid

        val property = StringBuilder() //添加特征属性

        val charaProp = bluetoothGattCharacteristic.properties
        if (charaProp and BluetoothGattCharacteristic.PROPERTY_READ > 0) {
            property.append("Read")
            property.append(" , ")
        }
        if (charaProp and BluetoothGattCharacteristic.PROPERTY_WRITE > 0) {
            property.append("Write")
            property.append(" , ")
        }
        if (charaProp and BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE > 0) {
            property.append("Write No Response")
            property.append(" , ")
        }
        if (charaProp and BluetoothGattCharacteristic.PROPERTY_NOTIFY > 0) {
            property.append("Notify")
            property.append(" , ")
        }
        if (charaProp and BluetoothGattCharacteristic.PROPERTY_INDICATE > 0) {
            property.append("Indicate")
            property.append(" , ")
        }
        if (property.length > 1) {
            property.delete(property.length - 2, property.length - 1)
        }
        if (property.isNotEmpty()) {
            holder.tvCharaType.text = property.toString()
        }



        //添加特征的点击事件
        holder.itemView.setOnClickListener {
            val charaProp: Int = bluetoothGattCharacteristic.properties
            val propList: MutableList<Int> = ArrayList()
            val propNameList: MutableList<String> = ArrayList()
            if (charaProp and BluetoothGattCharacteristic.PROPERTY_READ > 0) {
                propList.add(CharacteristicOperationFragment.PROPERTY_READ)
                propNameList.add("Read")
            }
            if (charaProp and BluetoothGattCharacteristic.PROPERTY_WRITE > 0) {
                propList.add(CharacteristicOperationFragment.PROPERTY_WRITE)
                propNameList.add("Write")
            }
            if (charaProp and BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE > 0) {
                propList.add(CharacteristicOperationFragment.PROPERTY_WRITE_NO_RESPONSE)
                propNameList.add("Write No Response")
            }
            if (charaProp and BluetoothGattCharacteristic.PROPERTY_NOTIFY > 0) {
                propList.add(CharacteristicOperationFragment.PROPERTY_NOTIFY)
                propNameList.add("Notify")
            }
            if (charaProp and BluetoothGattCharacteristic.PROPERTY_INDICATE > 0) {
                propList.add(CharacteristicOperationFragment.PROPERTY_INDICATE)
                propNameList.add("Indicate")
            }
            if (propList.size > 1) {
                AlertDialog.Builder(context)
                    .setTitle(context.getString(R.string.select_operation_type))
                    .setItems(
                        propNameList.toTypedArray()
                    ) { _, which ->
                        val characteristicOperationFragment: CharacteristicOperationFragment =
                            CharacteristicOperationFragment.newInstance(
                                bluetoothGattCharacteristic,
                                propList[which], bleDevice
                            )
                        //把fragment压入栈
                        //MyApplication.stack.push(characteristicOperationFragment)
                        (context as MainActivity).replaceFragment(characteristicOperationFragment)
                    }
                    .show()
            } else if (propList.size > 0) {
                val characteristicOperationFragment: CharacteristicOperationFragment =
                    CharacteristicOperationFragment.newInstance(
                        bluetoothGattCharacteristic,
                        propList[0], bleDevice
                    )
                (context as MainActivity).replaceFragment(characteristicOperationFragment)
            }
        }
    }

    override fun getItemCount() = characterList.size

}